package com.controller.manager;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.service.InternalService;

@Controller
@RequestMapping("/internal")
public class InternalController {

	@Resource
	private InternalService internalService;

	@RequestMapping(value = "/toInternalPage")
	public ModelAndView toInternal(HttpServletRequest request) {
		String username = (String) request.getSession().getAttribute("username");
		if (username == null || "".equals(username)) {
			return new ModelAndView("/login");
		}
		return new ModelAndView("/manager/internal");
	}

	@RequestMapping(value = "/addInternal", method = RequestMethod.POST)
	@ResponseBody
	public String addInternal(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {

		try {
			String contextPath = request.getScheme() // 当前链接使用的协议
					+ "://www.sijixiaoyuan.com"// "www.baodaoapp.com"// 服务器地址
					+ ":" + request.getServerPort() // 端口号
					+ request.getContextPath(); // 应用名称，如果应用名称为
			paramsMap.put("contextPath", contextPath);
			String pathString = request.getSession().getServletContext().getRealPath("ewm");
			System.out.println("pathString:" + pathString);
			paramsMap.put("pathString", pathString);
			int result = internalService.addInternal(paramsMap);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	// 删除
	@RequestMapping(value = "/deleteInternal", method = RequestMethod.POST)
	@ResponseBody
	public String deleteInternal(@RequestParam Map<String, String> paramsMap) {
		try {
			int result = internalService.deleteInternal(paramsMap);
			if (result >= 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	@RequestMapping(value = "/updateInternal", method = RequestMethod.POST)
	@ResponseBody
	public String updateInternal(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		try {
			String contextPath = request.getScheme() // 当前链接使用的协议
					+ "://www.sijixiaoyuan.com" //+ request.getRemoteHost()// "www.baodaoapp.com"// 服务器地址
					+ ":" + request.getServerPort() // 端口号
					+ request.getContextPath(); // 应用名称，如果应用名称为
			paramsMap.put("contextPath", contextPath);
			String pathString = request.getSession().getServletContext().getRealPath("ewm");
			System.out.println("pathString:" + pathString);
			paramsMap.put("pathString", pathString);
			int result = internalService.updateInternal(paramsMap);
			if (result > 0) {
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "error";
	}

	// 查询
	@RequestMapping(value = "/getInternal", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView getInternal(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> internals = internalService.getInternal(paramsMap);
			Map<String, String> internal = internalService.getInternalByUuid(paramsMap);
			request.setAttribute("internal", internal);
			request.setAttribute("internals", internals);
			return new ModelAndView("/yctfsn/index_shinei");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@RequestMapping(value = "/getInternalByParam", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getInternalByName(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = internalService.getInternalByName(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/getAllInterInfoByName", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> getAllInternalInfo(@RequestParam Map<String, String> paramsMap) {
		try {
			List<Map<String, String>> list = internalService.getAllInterInfoByName(paramsMap);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	// 查询
		@RequestMapping(value = "/getQrcode", method = RequestMethod.GET)
		@ResponseBody
		public ModelAndView getQrcode(HttpServletRequest request, @RequestParam Map<String, String> paramsMap) {
			try {
				List<Map<String, String>> qrcode = internalService.getQrcode(paramsMap);
				request.setAttribute("qrcode", qrcode);
				return new ModelAndView("/manager/allQrcode");
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
}
